<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
class Company extends Controller{
	public function index(){
        return view();
    }
    public function lists(){
        $data=Db::table('company')->select();
        foreach ($data as $key => $value){
            $data[$key]['imgs'] = explode('|',$data[$key]['imgs']);
            $data[$key]['types'] =implode('、',db('type')->where('id','in',explode(',',$value['typeid']))->column('tname'));
            $data[$key]['areaname'] =db('area')->where('id',$value['areaid'])->value('aname');
            $data[$key]['styles'] =implode('、',db('style')->where('id','in',explode(',',$value['styleid']))->column('sname'));
        }
        return view('',['data'=>$data]);
    }
    public function add(){
		//判断页面是否提交
		if(request()->isPost()){
            $icon=$this->saveimg('logo');
            $files = request()->file('image');
            $banner = request()->file('banner');
            $info="";
            foreach($files as $key => $picFile){
                if($picFile){
                    $info = $picFile->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public/static/images');
                    if($info){
                        $info=str_replace("\\","/",$info->getSaveName()).'|';
                    }
                }
                $info.=$info;
            }
            $bannerPath = '';
            foreach($banner as $key => $picFile){
                if($picFile){
                    $bannerPath = $picFile->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public/static/images');
                    if($info){
                        $bannerPath=str_replace("\\","/",$bannerPath->getSaveName()).'|';
                    }
                }
                $bannerPath.=$bannerPath;
            }
            $info =substr($info,0,strlen($info)-1);
            $bannerPath =substr($bannerPath,0,strlen($bannerPath)-1);
//            halt($info);
//			dump(input('post.')); //打印接收到的参数
            $temp = input('post.');
            $styleid = null;
            $typeid = null;
        if (array_key_exists('styleid',$temp)){
            $styleid = implode(',',$temp['styleid']);
        }
            if (array_key_exists('typeid',$temp)) {
                $typeid = implode(',', $temp['typeid']);
            }
			$data = [		//接受传递的参数
                'logo' => $icon,
                'name' => $temp['name'],
                'about' =>$temp['about'],
                'areaid' =>$temp['areaid'],
                'styleid' => $styleid,
                'typeid' =>$typeid,
                'desc' => $temp['desc'],
                'imgs'=>$info,
                'banner'=>$bannerPath,
            ];
		/*	Db('表名') 数据库助手函数*/
			if(Db('company') -> insert($data)){		//添加数据
				return $this->success('添加成功','lists');	//成功后跳转  lst 界面
			}else{
				return $this->error('添加失败');
			}
        }
        $area = db('area')->select();
		$style = db('style')->select();
		$type = db('type')->select();
        return view('',['area'=>$area,'style'=>$style,'type'=>$type]);
    }

    public function saveimg($inputName){
        $imgPath= '';
        $file = request()->file($inputName);
        if($file){
            $info = $file->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public/static/images');
            if($info){
                $imgPath=str_replace("\\","/",$info->getSaveName());
            }
        }
        return $imgPath;
    }
//修改
    public function edit(){
        if (request()->isPost()) {
            $logo = $this->chageImg('logo',input('oldplace'));
            $files = request()->file('image');
            $banner = request()->file('banner');
            $info=input('oldinfo');
            $bannerPath=input('banner');
            if ($files){
                foreach($files as $key => $picFile){
                    if($picFile){
                        $info = $picFile->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public/static/images');
                        if($info){
                            $info=str_replace("\\","/",$info->getSaveName()).'|';
                        }
                    }
                    $info.=$info;
                }
                $info =substr($info,0,strlen($info)-1);
            }
            if ($banner){
                foreach($banner as $key => $picFile){
                    if($picFile){
                        $bannerPath = $picFile->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public/static/images');
                        if($info){
                            $bannerPath=str_replace("\\","/",$bannerPath->getSaveName()).'|';
                        }
                    }
                    $bannerPath.=$bannerPath;
                }
                $bannerPath =substr($bannerPath,0,strlen($bannerPath)-1);
            }
            $temp = input('post.');
            $styleid = null;
            $typeid = null;
            if (array_key_exists('styleid',$temp)){
                $styleid = implode(',',$temp['styleid']);
            }
            if (array_key_exists('typeid',$temp)) {
                $typeid = implode(',', $temp['typeid']);
            }
            $data=[
                'logo'=>$logo,
                'name' => $temp['name'],
                'about' =>$temp['about'],
                'tel'=>$temp['tel'],
                'areaid' =>$temp['areaid'],
                'styleid' => $styleid,
                'typeid' =>$typeid,
                'desc'=>$temp['desc'],
                'imgs'=>$info,
                'banner'=>$bannerPath,
            ];

            if(db('company')->where('id',$temp['id'])->update($data)!==false){
                return $this->success('修改成功','lists');
            }else{
                return $this->error('修改失败');
            }
            return;
        }

        $id=input('id');
        $admins=db('company')->find($id);
        $area = db('area')->select();
        $style = db('style')->select();
        $type = db('type')->select();
        return view('',['admins'=>$admins,'area'=>$area,'style'=>$style,'type'=>$type]);

    }

    public function chageImg($imputName,$oldPath){
	    $place = $oldPath;
        $file = request()->file($imputName);
        if($file){
            $path = str_replace("\\","/",ROOT_PATH).'public/static/images/'.input('oldplace');//图片路径
            //var_dump($path);
            if (($oldPath!=null||$oldPath!='')&&file_exists($path)) {//判断图片路径是否存在
                unlink($path);//删除旧图片
            }
            $info = $file->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public/static/images');
            if($info){
                $place=str_replace("\\","/",$info->getSaveName());
            }
        }
        return $place;
    }
    public function del($id){
    	if(input('id')!=null){
    		$id = input('id');
    		if(Db('company')->delete($id)){
    			return $this->success('删除成功','lists');

    		}
    		else{
    			return $this->error('删除失败');
    		}
    		return;
    	}
    }
}